#!/bin/bash

# set for your product
ip_indexes=0
db_indexes=0
tbl_indexes=127


rep_cnf_file="rep.cnf.spam"
tmp_rep_cnf_file="${rep_cnf_file}.bak"
rm -rf $rep_cnf_file $tmp_rep_cnf_file

ngroup=0

for rip in `seq 0 $ip_indexes`
do
    ip=`printf "172.24.64.107"`

    for rdb in `seq 0 $db_indexes` # change to <0, 1> for table level
    do
        gidx=0
        let dbx=(rip-43)*2+rdb
        db=`printf "spam_manager"`
        #db=`printf "feel_%02d,feel_%02d" $dbx $(($dbx+1))`
        #echo "master|$ip|3306|fm$db|$db|feed_auc_count_|dump|dump123|mysql-bin.00000|106|/u01/drc_mysql" | tee -a $tmp_rep_cnf_file
        for rtbl in `seq 0 $tbl_indexes`
        do
            let tblx=rtbl
            tbl=`printf spamauction_%04d $tblx`
    	    echo "master|$ip|3306|M:$db:$ngroup|$db|$tbl|dump|dump123|mysql-bin.00000|106|/u01/cm5_spam" | tee -a $tmp_rep_cnf_file
            let nmaster=nmaster+1;

	        tbl=`printf spamuser_%04d $tblx`
	        echo "master|$ip|3306|M:$db:$ngroup|$db|$tbl|dump|dump123|mysql-bin.00000|106|/u01/cm5_spam" | tee -a $tmp_rep_cnf_file
            let nmaster=nmaster+1;
            let gidx+=1

            if [ $gidx -eq 8 ] ;then
                echo "slave|172.25.89.28|3306|S:db|cm5_spam|cm5_spam|repsync|repsync123|0|0" | tee -a $tmp_rep_cnf_file
                gidx=0
                let ngroup+=1
            fi    

        done
    done

    table_list=("usercandidates" "approvedhistory" "users")
	for tbl in ${table_list[@]}
	do
	    echo "master|$ip|3306|M:$db:ext|$db|$tbl|dump|dump123|mysql-bin.00000|106|/u01/cm5_spam" | tee -a $tmp_rep_cnf_file
        let nmaster=nmaster+1;
	done

done

# edit for your slave cnf
echo "slave|172.25.89.28|3306|S:db|cm5_spam|cm5_spam|repsync|repsync123|0|0" | tee -a $tmp_rep_cnf_file


start=1
row_diff=266
let end=start+row_diff

while read line 
do
    binfile=`echo "$line" | cut -f1`
    binpos=`echo "$line" | cut -f2`
    cat $tmp_rep_cnf_file | sed -n "${start},${end}p" | sed "s/mysql-bin.00000|/${binfile}|/; s/106/${binpos}/"  | tee -a $rep_cnf_file
    let start=end+1
    let end=start+row_diff
done < "sync_pos.log"

tail -1 $tmp_rep_cnf_file >> $rep_cnf_file
